package com.aplink.dellcommercial.fragments;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Message;

import com.aplink.dellcommercial.R;
import com.aplink.dellcommercial.configuration.DellController;
import com.aplink.dellcommercial.constant.FormDesignParameter;
import com.aplink.dellcommercial.webservices.DataResult;
import com.aplink.dellcommercial.webservices.ErrorCode;
import com.aplink.dellcommercial.webservices.Methods;
import com.aplink.dellcommercial.webservices.ServiceConstants.ForgetPasswordService;
import com.aplink.dellcommercial.webservices.WebServiceHelper;
import com.aplink.generic.GenericFragment;
import com.aplink.generic.constant.HandlerConstant;
import com.aplink.generic.handler.IHandleMessage;
import com.aplink.generic.pojo.ObjPropValue;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.util.validation.ControlValidation;

public class ResetPasswordFragment extends GenericFragment implements
        IHandleMessage {
    private AlertDialog.Builder builder;
    private boolean hasResetPassword = false;
    private final DialogInterface.OnClickListener mOnClickListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick( DialogInterface dialog, int which ) {
            if ( ResetPasswordFragment.this.hasResetPassword ) {
                ResetPasswordFragment.this.finishActivity();
            }
        }
    };

    private void handleDataResult( DataResult dataResult ) {
        if ( dataResult.getMethodName().equals( Methods.resetPass ) ) {
            if ( ( dataResult.getErrorCode() == null )
                    || ( dataResult.getErrorCode() == ErrorCode.NO_ERROR ) ) {
                final String result = (String) dataResult.getData();
                if ( result.equals( ForgetPasswordService.RESET_SUCCESS ) ) {
                    this.builder
                            .setMessage( "Your password has been sent to your inbox" );
                    this.builder.create().show();
                    this.hasResetPassword = true;
                } else {
                    this.builder
                            .setMessage( "Your e-mail is not registered in our program" );
                    this.builder.create().show();
                    this.hasResetPassword = false;
                }
            }
        }
    }

    private void submitResetPasswordCommand() {
        if ( DellController.isOnline() ) {
            for ( ObjPropValue value : getObjPropValues() ) {
                String mappingName = value.getObjectPropID();
                if ( mappingName
                        .equalsIgnoreCase( FormDesignParameter.ForgetPassword.VALIDATION_EMAIL_ADDRESS ) ) {
                    String email = (String) value.getAttribute().getValue();
                    WebServiceHelper.getInstance().resetPassword( getActivity(),
                            this.getHandler(), email, this.getObjPropList() );
                    break;
                }
            }
        }
    }

    @Override
    protected void onHandleControlClickEvent( ControlSpec controlSpec ) {
        final String validateGroup = controlSpec.getValidationGroup();
        if ( validateGroup
                .equals( FormDesignParameter.ForgetPassword.VALIDATION_GROUP_RESET_PASSWORD ) ) {
            if ( ControlValidation.isFormValidate( this, validateGroup ) ) {
                submitResetPasswordCommand();
            }
        }
    }

    @Override
    public void onActivityCreated( Bundle savedInstanceState ) {
        super.onActivityCreated( savedInstanceState );
        this.initData();
        this.loadDataToForm();
        this.builder = new AlertDialog.Builder( this.getApplicationContext() );
        this.builder.setIcon( android.R.drawable.ic_dialog_alert )
                .setTitle( "Forget Password" ).setCancelable( false )
                .setPositiveButton( android.R.string.ok, this.mOnClickListener );
        this.getHandler().setHandleMessage( this );
    }

    @Override
    public void onHandleMessage( Message msg ) {
        if ( msg.what == HandlerConstant.HTTP_TASK_RESULT ) {
            final DataResult result = (DataResult) msg.obj;
            this.handleDataResult( result );
        }
    }

    @Override
    public int getContentView() {
        return R.layout.aplink_dellcommercial_forget_password;
    }

    @Override
    public int getFormDesign() {
        return R.raw.form_design_forget_password;
    }

    @Override
    public int getContentData() {
        return 0;
    }

    @Override
    public String getTable() {
        return null;
    }
}
